<?
require_once(dirname(__FILE__) . '/../../classes/report_interface.inc.php');

abstract class LoggerModuleBrowsersReportBaseClass 
{
    protected $database;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
    }

    protected function GetBrowsersData($date1, $date2, $start, $count)
    {
        $date1 = '\'' . mysql_escape_string($date1) . '\'';
        $date2 = '\'' . mysql_escape_string($date2) . '\'';

        $SelectSQL = 'SELECT `browser`, `s`.`string` AS `browserStr`, SUM(`summ`) AS `summ` FROM (';
        $SelectSQL .= 'SELECT `b`.`browser`, SUM(`b`.`count`) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_browsers_archive` `b` ';
        $SelectSQL .= 'WHERE `b`.`date`>='.$date1.' AND `b`.`date`<='.$date2.' ';
        $SelectSQL .= 'GROUP BY `b`.`browser` ';

        $SelectSQL .= 'UNION ALL ';

        $SelectSQL .= 'SELECT `b`.`browser`, COUNT(*) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_browsers` `b` ';
        $SelectSQL .= 'WHERE `b`.`date`>='.$date1.' AND `b`.`date`<='.$date2.' ';
        $SelectSQL .= 'GROUP BY `b`.`browser` ';

        $SelectSQL .= ') AS `__temp_query` ';
        $SelectSQL .= 'LEFT JOIN `[PREF]module_browsers_strings` `s` ON `s`.`id`=`__temp_query`.`browser` ';
        $SelectSQL .= 'GROUP BY `browser` ';
        $SelectSQL .= 'ORDER BY `summ` DESC ';
        $SelectSQL .= 'LIMIT '.$start.','.$count;

        $data = array();
        $result = $this->database->Query($SelectSQL);
        if (!$result)
             trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        while ($row = $this->database->FetchAssoc($result))
        {
            $data[] = $row;
        }
        $this->database->FreeResult($result);

        return $data;
    }

    protected function GetVerData($date1, $date2, $browser, $start, $count)
    {
        $date1 = '\'' . mysql_escape_string($date1) . '\'';
        $date2 = '\'' . mysql_escape_string($date2) . '\'';
        $browser = '\'' . mysql_escape_string($browser) . '\'';

        $SelectSQL = 'SELECT `ver`, `s`.`string` AS `verStr`, SUM(`summ`) AS `summ` FROM (';
        $SelectSQL .= 'SELECT `b`.`ver`, SUM(`b`.`count`) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_browsers_archive` `b` ';
        $SelectSQL .= 'WHERE `b`.`date`>='.$date1.' AND `b`.`date`<='.$date2.' AND `b`.`browser`='.$browser.' ';
        $SelectSQL .= 'GROUP BY `b`.`ver` ';

        $SelectSQL .= 'UNION ALL ';

        $SelectSQL .= 'SELECT `b`.`ver`, COUNT(*) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_browsers` `b` ';
        $SelectSQL .= 'WHERE `b`.`date`>='.$date1.' AND `b`.`date`<='.$date2.' AND `b`.`browser`='.$browser.' ';
        $SelectSQL .= 'GROUP BY `b`.`ver` ';

        $SelectSQL .= ') AS `__temp_query` ';
        $SelectSQL .= 'LEFT JOIN `[PREF]module_browsers_strings` `s` ON `s`.`id`=`__temp_query`.`ver` ';
        $SelectSQL .= 'GROUP BY `ver` ';
        $SelectSQL .= 'ORDER BY `summ` DESC ';
        $SelectSQL .= 'LIMIT '.$start.','.$count;

        $data = array();
        $result = $this->database->Query($SelectSQL);
        if (!$result)
             trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        while ($row = $this->database->FetchAssoc($result))
        {
            $data[] = $row;
        }
        $this->database->FreeResult($result);

        return $data;
    }
}


class LoggerModuleBrowsersReport extends LoggerModuleBrowsersReportBaseClass implements LoggerReportInterface
{
    public function __construct(LoggerDatabase $database) 
    {
        parent::__construct($database);
    }

    public function GetReport($root, $date1, $date2, $params = false)
    {
        $limit_start = 0;
        $limit_count = 100;

        $xml = $root->ownerDocument;

        $name = $xml->createElement('report_name', 'Браузеры посетителей');
        $root->appendChild($name);

        $data = $this->GetBrowsersData($date1, $date2, $limit_start, $limit_count);
        $reportData = $xml->createElement('report_data');
        $root->appendChild($reportData);

        foreach($data as $row)
        {
            $dataRow = $xml->createElement('row');
            $reportData->appendChild($dataRow);
            $dataRow->setAttribute('browser', $row['browser']);
            $dataRow->setAttribute('browserStr', $row['browserStr']);
            $dataRow->setAttribute('summ', $row['summ']);
        }

        $xsl = dirname(__FILE__) . '/templates/browsers.xsl';
        $pi = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$xsl.'"');
        $xml->insertBefore($pi, $root);
        return true;
    }
}


class LoggerModuleBrowsersExpandReport extends LoggerModuleBrowsersReportBaseClass implements LoggerReportInterface
{
    public function __construct(LoggerDatabase $database) 
    {
        parent::__construct($database);
    }

    public function GetReport($root, $date1, $date2, $params = false)
    {
        $limit_start = 0;
        $limit_count = 100;
        
        $browser = false;
        if (isset($params['browser']) && $params['browser'] != '')
        {
            $browser = $params['browser'];
        }

        $xml = $root->ownerDocument;

        $data = $this->GetVerData($date1, $date2, $browser, $limit_start, $limit_count);

        $reportData = $xml->createElement('report_data');
        $root->appendChild($reportData);

        foreach($data as $row)
        {
            $dataRow = $xml->createElement('row');
            $reportData->appendChild($dataRow);
            $dataRow->setAttribute('browser', $browser);
            $dataRow->setAttribute('ver', $row['ver']);
            $dataRow->setAttribute('verStr', $row['verStr']);
            $dataRow->setAttribute('summ', $row['summ']);
        }

        $xsl = dirname(__FILE__) . '/templates/browsers_expand.xsl';
        $pi = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$xsl.'"');
        $xml->insertBefore($pi, $root);
        return true;
    }
}
